﻿
namespace NTLib.WPF.UTest.Themes
{
    using Application.Themes;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using Test.Resources;
    using WPF.Themes;

    [TestClass]
    public class ThemeUTest : BaseTestClass
    {
        #region Fields

        private const string CATEGORY = "WPF.Themes";

        #endregion

        #region Properties

        /// <summary>
        /// Themes the create themes.
        /// </summary>
        [TestCategory(CATEGORY)]
        [TestMethod()]
        public void Theme_CreateThemes()
        {
            var timeout = base.CreateTimeout(15);
            using (base.LockTest(timeout.Token))
            {
                try
                {
                    string dark = "Dark";
                    string light = "Light";

                    Collection<IResourceDictionary> themes = new Collection<IResourceDictionary>();
                    ThemeManager.DictionaryRoot = themes;
                    ThemeContainer container = new ThemeContainer();
                    container.Themes = new ObservableCollection<ThemeDictionary>();

                    var darkRess = new ThemeResourceDictionary();
                    container.Themes.Add(new ThemeDictionary() { ThemeName = dark, Dictionary = darkRess });

                    var darkRess2 = new ThemeResourceDictionary();
                    container.Themes.Add(new ThemeDictionary() { ThemeName = dark, Dictionary = darkRess2 });

                    var lightRess = new ThemeResourceDictionary();
                    container.Themes.Add(new ThemeDictionary() { ThemeName = light, Dictionary = lightRess });

                    container.Themes.Add(new ThemeDictionary() { Dictionary = lightRess });

                    Assert.IsNotNull(ThemeManager.Themes);
                    var ths = ThemeManager.Themes.ToArray();
                    Assert.AreEqual(3, ths.Length);
                    Assert.IsTrue(ths.Contains(dark.ToUpperInvariant()));
                    Assert.IsTrue(ths.Contains(light.ToUpperInvariant()));
                    Assert.IsTrue(ths.Contains(ThemeManager.DEFAULT));
                }
                finally
                {
                    ThemeManager.Reset();
                }
            }
        }

        /// <summary>
        /// Themes the set themes.
        /// </summary>
        [TestCategory(CATEGORY)]
        [TestMethod()]
        public void Theme_SetThemes()
        {
            var timeout = base.CreateTimeout(15);
            using (base.LockTest(timeout.Token))
            {
                try
                {
                    string dark = "Dark";
                    string light = "Light";

                    Collection<IResourceDictionary> themes = new Collection<IResourceDictionary>();
                    ThemeManager.DictionaryRoot = themes;
                    ThemeContainer container = new ThemeContainer();
                    container.Themes = new ObservableCollection<ThemeDictionary>();

                    var darkRess = new ThemeResourceDictionary() ;
                    container.Themes.Add(new ThemeDictionary() { ThemeName = dark, Dictionary = darkRess });

                    var darkRess2 = new ThemeResourceDictionary();
                    container.Themes.Add(new ThemeDictionary() { ThemeName = dark, Dictionary = darkRess2 });

                    var lightRess = new ThemeResourceDictionary();
                    container.Themes.Add(new ThemeDictionary() { ThemeName = light, Dictionary = lightRess });

                    var common = new ThemeResourceDictionary();
                    container.Themes.Add(new ThemeDictionary() { ThemeName = dark, Dictionary = common });
                    container.Themes.Add(new ThemeDictionary() { ThemeName = light, Dictionary = common });
                    
                    var defaultOne = new ThemeResourceDictionary();
                    container.Themes.Add(new ThemeDictionary() { Dictionary = defaultOne });

                    Assert.IsNotNull(ThemeManager.Themes);
                    var ths = ThemeManager.Themes.ToArray();
                    Assert.AreEqual(3, ths.Length);
                    Assert.IsTrue(ths.Contains(dark.ToUpperInvariant()));
                    Assert.IsTrue(ths.Contains(light.ToUpperInvariant()));
                    Assert.IsTrue(ths.Contains(ThemeManager.DEFAULT));
                    Assert.IsTrue(themes.Contains(defaultOne));

                    Assert.AreEqual(1, themes.Count);

                    ThemeManager.SetThemeAsync(dark, timeout.Token).Wait(timeout.Token);

                    Assert.AreEqual(3, themes.Count);
                    Assert.IsFalse(themes.Contains(defaultOne));
                    Assert.IsTrue(themes.Contains(darkRess));
                    Assert.IsTrue(themes.Contains(darkRess2));
                    Assert.IsTrue(themes.Contains(common));


                    ThemeManager.SetThemeAsync(light, timeout.Token).Wait(timeout.Token);

                    Assert.AreEqual(2, themes.Count);
                    Assert.IsFalse(themes.Contains(darkRess));
                    Assert.IsFalse(themes.Contains(darkRess2));
                    Assert.IsFalse(themes.Contains(defaultOne));

                    Assert.IsTrue(themes.Contains(lightRess));
                    Assert.IsTrue(themes.Contains(common));
                }
                finally
                {
                    ThemeManager.Reset();
                }
            }
        }

        #endregion
    }
}
